Final Exam Review

Review Reading

Vocabulary

Operating System
Memory Management
Optimize
Platform
User-Interface
GUI
CLI
DLL
Analysis
Design
Implementation
Maintenance
Hardware
Software
Primary Memory
Secondary Memory
Cache Memory
CPU
RAM
Device Driver
Interrupt
Buffer
Virtual Machine
Multi-tasking
Error
Syntax error
Run-time error
Parameter
String
double
int
char
method
constructor
class
object
Algorithm
Greedy Algorithm
Exhaustive Search
Searching
Bubble sort
Selection sort
Efficiency
Correctness
Reliability
Flexibility
Data-structure
Variable
Object
Array
Linear Array
2-D Array
Parallel Arrays
Text File
Data File
Media File
Scope
Global variable
Local variable
Loop (for.. , while..)
decision (if..)
indexOf
substring
equal,equalIgnoreCase
charAt
void , return

Review Questions

  1. Explain the difference between int and double variables.
  2. Explain how an int variable can overflow, and describe the result of this problem.
  3. What is the essential difference between hardware and software?
  4. Ekplain why software development is broken into 4 stages:
    Analysis , Design , Implementation (creating solution), Maintenance
  5. What are the 4 essential components of any computing system?
  6. If a sequential search in an array of 1 million items taks an average of 10 sec, what is the average time required for searching 2 million items?
  7. If a bubble-sort in an array of 10000 items takes 10 sec, how long will a bubble-sort of 20000 items take?
  8. Global variables have a larger scope than local variables. So why would a programmer want to use local variables, if they are more limited?
  9. Explain the difference between a syntax error and a run-time error, including a common example of each.
  10. Explain why the following would cause a compile-time error:
    public void convert(double euros)
    { double answer = 1.35 * euros;
    return answer;
    }
  11. Explain what "case-sensitive" means, and why this is important in Java programs.
  12. Explain the difference between parallel arrays and a 2-D array, including an example of where each idea is useful.
  13. Explain the difference between a bubble-sort and a selection-sort. Which one will finish sooner if only one data item is in the wrong place? Why?
  14. Explain the how classes and objects are related, and also how they are different.
  15. Assuming that the size of an MP3 music file is 4 MB, calculate the number of MP3 music files that will fit in 2 GigaBytes.
  16. Describe a program where you used Object Oriented Programming techniques. Explain why this made the program easier to write.
  17. Explain the difference between a Graphical User Interface and a Command Line Interface.
  18. Describe a situation where a program might need to use data files. Explain why the program could not have all the data included inside the Java program.
  19. Explain why the use of methodsmakes a program easier to write.
  20. Explain why the following command must cause an error.
    data[data.length] = data[data.length-1];
  21. Explain why a TV is not a computer according to the classical definition.
  22. Describe how a compiler helps the programmer write correct programs.
  23. Describe how a good IDE can help the programmer write correct programs.
  24. Describe two important functions of an operating system - these must be tasks or features that are NOT contained directly in an application.
  25. Explain the relationship between primary memory and secondary memory.

Programming Problems

26. Study the following code fragment.
**<span style="font-size: 12pt;">int</span>**<span style="font-size: 12pt;"> x = 12;</span>
**<span style="font-size: 12pt;">int </span>**<span style="font-size: 12pt;">y = 10;</span>
**<span style="font-size: 12pt;">int </span>**<span style="font-size: 12pt;">ans = x / y;</span>
**<span style="font-size: 12pt;">output(</span>**<span style="font-size: 12pt;">ans);</span>
(a) Explain why this does not print 1.2
(b) Describe the necessary changes so that it prints the correct answer (1.2).
27. Assume that the simple (linear) array PRICES[] contains 100 double values.
(a) Write a method called SORT which would sort the numbers in descending order
(from largest to smallest).

(b) Write a method that will count the number of prices that are greater than 50.
The method should return this number.
28. Assume that a two-dimensional array named MONEY contains 3 rows and 7 columns.
The numbers in the array represent the money spent by a businessman on breakfast,
lunch, and dinner in one week - for example:

0
1
2
3
4
5
6
0
0.00
0.00
2.99
0.00
5.95
0.00
0.00
1
10.50
12.99
15.00
9.99
0.00
5.50
21.95
2
25.00
20.00
15.00
12.99
29.50
19.50
7.50
Write a method that adds up all the prices in the array and outputs the total.
29. The following method is supposed to validate a String
that contains the abbreviation of a month.
It should return true if the name is valid (acceptable),
but return false if the name is incorrect.
For example, checkMonth("Jan") should return true,
but checkMonth("Okt") should be false.
public boolean checkMonth(String name)
{
String[] months = {"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec" };
for (int x = 0; x < 12; x = x+1)
{
if ( name.equals(months[x]) )
{ return true; }
else
{ return false; }
}
}
(a) Assume that the command checkMonth("Mar") is executed.
Explain why the checkMonth method does not return the correct answer.
(b) Rewrite the method so that it will always return the correct result.
30. The following method reads numbers from a sequential text-file.
public int[] readNumbers()
{
int[] results = new int[1000];

try
{ BufferedReader file =new BufferedReader(
new FileReader("nums.txt") );
int pos = 0;
while (file.ready())
{
String data = file.readLine();
int num = Integer.parseInt(data);
results[pos] = num;
pos = pos + 1;
}
file.close();
results[pos] = -999;
}
catch(IOException ex){output(ex.toString()); }
return results;
}

(a) Explain the purpose of the number -999.
(b) Explain why try..catch.. are required when reading a file.
(c) Assume the file contains 50 numbers, and the following command was executed:
int[] numbers = readNumbers();
(i) Write a method that adds up all the numbers in the array.

(ii) Write a method to find the largest number in the list.
(d) Assume the array numbers[] contains 50 numbers.
Write a method called sort that will sort
the numbers into descending order,
placing the largest numbers at the beginning of the list
and the smallest numbers at the end of the list.
31. Assume that the file NAMES.TXT contains a long list of names -
thousands of entries. Write a method that will search for a name in the file.
If the name is found, the method prints "found" - otherwise it prints "missing".
32. Write a method that accepts a String as a parameter.
The method looks through the String and counts the number of times
that it finds the letter "s". It returns this number.
33. TRACE this algorithm and write everything that will be printed.

int x = 10;
double d = 20.0;
String s = "30.00";

System.out.println(x + d);
System.out.println(d + s);
System.out.println(x + d + " = " + s)

34. Write a Java method that accepts 3 numbers as parameters,
decides which of the numbers is the largest, and returns that number.
35. Assume that a Java program is supposed to input a user's e-mail address.
The address will be input as follows: String email = input("E-mail address:");
The address should look something like this: username@server.com

(a) Explain the difference between validating and verifying user input.
Decide which is more appropriate for checking the e-mail address and explain why.
(b) Write a single Java command to change the e-mail address to all CAPITALS.
(c) Write a method to check whether the user has typed a reasonable address.
Your method should check that:
(i) the address contains the '@' character
(ii) the address contains a period and it is after the '@' character
(iii) there are at least 2 characters between the '@' and the period
(iv) there are at least 3 characters following the period
The method should return true if the address is okay - otherwise return false.



Program Design Discussion

36. A data file contains a record for each employee in a small business (under 100 employees). For each employee, it contains 4 fields : name , phone , e-mail , salary.
Assume that the LOAD method copies all the data from the file into a 2-D array that looks like this:
String[][] data
Adams, Alex
123-4567
AA@bbb.com
27500
Bonkers, Bozo
0173-2233445
Bozo@clowns.info
999999.99
Chaplin, Charley
unlisted
charley@chaplin.de
1000
Duck, Donald
1-800-111-2222
quack@quack.tv
123456789
....
....
....
....
After the LOAD method runs, the variable dataCount contains the number of records in the data array. If the array contained only the 4 records shown, then dataCount would be 4.
(a) The salary numbers must be stored as String values. Explain why this will make it more difficult to write a method that adds up all the salaries.
(b) In clear, exact English, describe the algorithm that would accept an e-mail address as a parameter and return the corresponding name.
(c) Describe two advantages of saving this data in a computer than writing it on paper.
(d) Describe one advantage of using a Java program to process this data rather than using a standard application, like a spreadsheet.
(e) Write a method that finds the highest salary in the list, and prints the name of the employee with the highest salary.